// api_server/router_handler/daily_summary.js
const db = require('../db/index');
exports.generateDailySummary = async (req, res) => {
  const userId = req.user.id;
  try {
    // 获取日程安排
    const schedules = await getSchedules(userId);
    // 获取会议纪要
    const meetings = await getMeetings(userId);
    // 获取消费记录
    const consumptions = await getConsumptions(userId);

    // 生成每日总结
    const summary = {
      schedules,
      meetings,
      consumptions
    };

    const summaryJson = JSON.stringify(summary);

    res.send({
      status: 0,
      message: '生成每日总结成功',
      data: summaryJson
    });
  } catch (error) {
    res.cc(`生成每日总结失败：${error.message}`);
  }
};

// 获取日程安排
function getSchedules(userId) {
  return new Promise((resolve, reject) => {
    const sql = 'SELECT * FROM schedules WHERE user_id = ? AND DATE(start_time) = CURDATE()';
    db.query(sql, [userId], (err, results) => {
      if (err) {
        reject(err);
      } else {
        resolve(results);
      }
    });
  });
}

// 获取会议纪要
function getMeetings(userId) {
  return new Promise((resolve, reject) => {
    const sql = 'SELECT * FROM meeting_minutes WHERE user_id = ? AND DATE(time) = CURDATE()';
    db.query(sql, [userId], (err, results) => {
      if (err) {
        reject(err);
      } else {
        resolve(results);
      }
    });
  });
}

// 获取消费记录
function getConsumptions(userId) {
  return new Promise((resolve, reject) => {
    const sql = 'SELECT * FROM consumption_records WHERE user_id = ? AND DATE(time) = CURDATE()';
    db.query(sql, [userId], (err, results) => {
      if (err) {
        reject(err);
      } else {
        resolve(results);
      }
    });
  });
}
